<template>
  <div class="app-container">
    <!-- 资产 -->

    <el-row :gutter="24">
      <el-col :lg="24" :xs="24">
        <div class="search-bar">
          <!-- 搜索 -->
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="80">
            <el-form-item label="资产编号" prop="code">
              <el-input
                v-noSpace
                v-model="queryParams.code"
                clearable
                class="!w-[192px]"
                @keyup.enter="handleQuery"
                placeholder="请输入资产编号"
              />
            </el-form-item>
            <el-form-item label="资产名称" prop="name">
              <el-input
                v-noSpace
                v-model="queryParams.name"
                clearable
                placeholder="请输入资产名称"
                class="!w-[192px]"
                @keyup.enter="handleQuery"
              />
            </el-form-item>

            <el-form-item label="类别" prop="type">
              <el-select
                v-model="queryParams.type"
                clearable
                filterable
                placeholder="请输入类别"
                class="!w-[192px]"
                @change="handleQuery"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="型号" prop="model">
              <el-input
                v-noSpace
                v-model="queryParams.model"
                clearable
                placeholder="请输入型号"
                class="!w-[192px]"
                @keyup.enter="handleQuery"
              />
            </el-form-item>

            <el-form-item label="资产状态" prop="status">
              <el-select
                v-model="queryParams.status"
                clearable
                filterable
                placeholder="请输入资产状态"
                class="!w-[192px]"
                @change="handleQuery"
              >
                <el-option
                  v-for="item in assetStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
              <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
              <el-button
                icon="Plus"
                type="success"
                @click="handleAdd({}, 'add')"
                v-hasPerm="'sys:basketHandling:edit'"
              >
                新增资产
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-card shadow="never">
          <el-table
            v-loading="loading"
            show-overflow-tooltip
            :tooltip-formatter="tableRowFormatter"
            height="300px"
            :data="pageData"
            @row-click="pageClick"
          >
            <el-table-column prop="code" label="资产编号" />
            <el-table-column prop="name" label="资产名称" />
            <el-table-column prop="type" label="类别">
              <template #default="scope">
                <div>
                  {{ typeFormat(scope.row.type) }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="model" label="型号" />
            <el-table-column prop="value" label="资产价值" />
            <el-table-column prop="purchaseDate" label="购置日期" />

            <el-table-column prop="usePosition" label="存放位置" />
            <el-table-column prop="remark" label="备注" />
            <el-table-column prop="status" label="状态">
              <template #default="scope">
                <div>
                  {{ statusFormat(scope.row.status) }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="280" fixed="right">
              <template #default="scope">
                <el-button
                  size="small"
                  link
                  @click.stop="handleMaintain(scope.row, 'add')"
                  v-hasPerm="'sys:basketHandling:maintain:edit'"
                >
                  新增保养
                </el-button>
                <el-button
                  size="small"
                  link
                  type="primary"
                  @click.stop="handleAdd(scope.row, 'edit')"
                  v-hasPerm="'sys:basketHandling:edit'"
                >
                  修改
                </el-button>
                <el-button
                  size="small"
                  link
                  type="primary"
                  @click.stop="handleAdd(scope.row, 'info')"
                >
                  查看
                </el-button>
                <el-button
                  size="small"
                  link
                  type="danger"
                  @click.stop="handleDelete(scope.row)"
                  v-hasPerm="'sys:basketHandling:del'"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="handleQuery"
          />
        </el-card>

        <el-card shadow="never" class="mt10">
          <el-table
            v-loading="loading"
            show-overflow-tooltip
            :tooltip-formatter="tableRowFormatter"
            :data="upkeepData"
          >
            <el-table-column prop="assetCode" label="资产编号" />
            <el-table-column prop="code" label="保养编号" />
            <el-table-column prop="maintenanceTime" label="保养时间" />
            <el-table-column prop="nextMaintenanceTime" label="下次保养时间" />
            <el-table-column prop="maintenanceRecord" label="保养记录" />
            <el-table-column prop="dept" label="部门">
              <template #default="scope">
                <div>{{ departmentFormat(scope.row.dept) }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="maintenanceUser" label="保养人" />
            <el-table-column prop="status" label="保养状态">
              <template #default="scope">
                <div>{{ upkeepStatusFormat(scope.row.status) }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" />
            <el-table-column label="操作" width="200" fixed="right">
              <template #default="scope">
                <!-- <el-button size="small" @click.stop="handleMaintain(scope.row)">新增保养</el-button> -->
                <el-button
                  size="small"
                  type="primary"
                  link
                  @click.stop="handleMaintain(scope.row, 'edit')"
                  v-hasPerm="'sys:basketHandling:maintain:edit'"
                >
                  修改
                </el-button>
                <el-button
                  size="small"
                  type="primary"
                  link
                  @click.stop="handleMaintain(scope.row, 'info')"
                >
                  查看
                </el-button>
                <el-button
                  size="small"
                  type="danger"
                  link
                  @click.stop="upkeepHandleDelete(scope.row)"
                  v-hasPerm="'sys:basketHandling:maintain:del'"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <!-- 新增/修改弹窗 -->
    <el-dialog
      v-model="dialogVisible.visible"
      :title="dialogVisible.title"
      width="1000px"
      @close="handleCloseDialog"
    >
      <el-form
        ref="dialogFormDataRef"
        :disabled="dialogVisible.type == 'info' ? true : false"
        :model="dialogFormData"
        :rules="rules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="资产名称" prop="name">
              <el-input
                v-noSpace
                v-model="dialogFormData.name"
                :maxlength="30"
                placeholder="请输入资产名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别" prop="type">
              <el-select
                v-model="dialogFormData.type"
                placeholder="请输入类别"
                style="width: 250px"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌" prop="brand">
              <el-input
                v-noSpace
                v-model="dialogFormData.brand"
                :maxlength="30"
                placeholder="请输入品牌"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号" prop="model">
              <el-input
                v-noSpace
                v-model="dialogFormData.model"
                :maxlength="30"
                placeholder="请输入型号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计量单位" prop="unit">
              <el-input v-noSpace v-model="dialogFormData.unit" placeholder="请输入计量单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产价值" prop="value">
              <el-input
                v-noSpace
                v-model.number="dialogFormData.value"
                placeholder="请输入资产价值"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="购置日期" prop="purchaseDate">
              <el-date-picker
                v-model="dialogFormData.purchaseDate"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用年限" prop="useYears">
              <el-input
                v-noSpace
                v-model.number="dialogFormData.useYears"
                placeholder="请输入使用年限"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保修期限" prop="reportTime">
              <el-date-picker
                v-model="dialogFormData.reportTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择日期"
              />
            </el-form-item>
            <!-- <el-input v-noSpace v-model.number="dialogFormData.reportTime" placeholder="请输入保修期限" /> -->
            <!-- </el-form-item> -->
          </el-col>
          <el-col :span="12">
            <el-form-item label="报废时间" prop="scrapDate">
              <el-date-picker
                v-model="dialogFormData.scrapDate"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplier">
              <el-input
                v-noSpace
                v-model="dialogFormData.supplier"
                :maxlength="50"
                placeholder="请输入供应商"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产归属部门" prop="assetCategory">
              <el-select v-model="dialogFormData.assetCategory" placeholder="请输入资产归属部门">
                <el-option
                  v-for="item in department"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用人" prop="user">
              <el-input
                v-noSpace
                v-model="dialogFormData.user"
                :maxlength="20"
                placeholder="请输入使用人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用部门" prop="useDept">
              <el-input v-noSpace v-model="dialogFormData.useDept" placeholder="请输入使用部门" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存放位置" prop="usePosition">
              <el-input
                v-noSpace
                v-model="dialogFormData.usePosition"
                placeholder="请输入存放位置"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产状态" prop="status">
              <el-select v-model="dialogFormData.status" placeholder="请输入资产状态">
                <el-option
                  v-for="item in assetStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-noSpace
                v-model="dialogFormData.remark"
                :maxlength="300"
                :rows="4"
                type="textarea"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>
          <el-col v-if="dialogVisible.type != 'info'" :span="24">
            <span class="dialog-footer df">
              <el-button @click="dialogVisible.visible = false">取 消</el-button>
              <el-button v-debounce="handleSave" type="primary">提 交</el-button>
            </span>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <!-- 保养弹窗 -->
    <el-dialog
      v-model="upkeepVisible.visible"
      :title="upkeepVisible.title"
      width="1000px"
      @close="upkeepCloseDialog"
    >
      <el-form
        ref="upkeepFormDataRef"
        :model="upkeepFormData"
        :disabled="upkeepVisible.type == 'info' ? true : false"
        :rules="upkeepRules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="保养部门" prop="dept">
              <el-select v-model="upkeepFormData.dept" placeholder="请输入保养部门">
                <el-option
                  v-for="item in department"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保养状态" prop="status">
              <el-select v-model="upkeepFormData.status" placeholder="请输入保养状态">
                <el-option
                  v-for="item in upkeepStatus"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保养时间" prop="maintenanceTime">
              <el-date-picker
                v-model="upkeepFormData.maintenanceTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下次保养时间" prop="nextMaintenanceTime">
              <el-date-picker
                v-model="upkeepFormData.nextMaintenanceTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保养人" prop="maintenanceUser">
              <el-input
                v-noSpace
                v-model="upkeepFormData.maintenanceUser"
                :maxlength="20"
                placeholder="请输入保养人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="保养记录" prop="maintenanceRecord">
              <el-input
                v-noSpace
                v-model="upkeepFormData.maintenanceRecord"
                :maxlength="500"
                :rows="4"
                type="textarea"
                placeholder="请输入保养记录"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-noSpace
                v-model="upkeepFormData.remark"
                :maxlength="300"
                :rows="4"
                type="textarea"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-col v-if="upkeepVisible.type != 'info'" :span="24">
          <span class="dialog-footer df">
            <el-button @click="upkeepVisible.visible = false">取 消</el-button>
            <el-button v-debounce="upkeepSave" type="primary">提 交</el-button>
          </span>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import assetManagementAPI, {
  assetManagementPageQuery,
  assetManagementPageVO,
  maintenancePageVO,
  maintenancePageQuery,
} from "@/api/assetManagement/assetManagement";
import configApi, { ConfigType } from "@/api/config/config";
import { FormItemRule } from "element-plus/es";
import { tableRowFormatter } from "@/utils/index";
defineOptions({
  name: "User",
  inheritAttrs: false,
});
// 加载loading
const loading = ref(false);
// 分页总数
const total = ref(0);
// 搜索ref
const queryFormRef = ref(ElForm);
// 一级弹窗ref
const dialogFormDataRef = ref(ElForm);
//一级弹窗
const dialogVisible = reactive({
  visible: false,
  title: "",
  type: "",
});

//一级表单验证
const rules = reactive<Record<string, FormItemRule[]>>({
  assetId: [{ required: true, message: "请输入资产编号", trigger: "blur" }],
  name: [{ required: true, message: "请输入资产名称", trigger: "blur" }],
  type: [{ required: true, message: "请输入类别", trigger: "blur" }],
  brand: [{ required: true, message: "请输入品牌", trigger: "blur" }],
  model: [{ required: true, message: "请输入型号", trigger: "blur" }],
  unit: [{ required: true, message: "请输入计量单位", trigger: "blur" }],
  value: [{ required: true, message: "资产价值是必填项", trigger: "blur" }],
  purchaseDate: [{ required: true, message: "请选择购置日期", trigger: "change" }],
  usageYears: [{ required: true, message: "使用年限是必填项", trigger: "blur" }],
  usePosition: [{ required: true, message: "请输入存放位置", trigger: "blur" }],
  status: [{ required: true, message: "请选择状态", trigger: "change" }],
});
// 查询参数
const queryParams = reactive<assetManagementPageQuery>({
  pageNum: 1,
  pageSize: 10,
});
//资产状态
const assetStatusList = ref([
  {
    label: "正常使用",
    value: 0,
  },
  {
    label: "维修中",
    value: 1,
  },
  {
    label: "已报废",
    value: 2,
  },
  {
    label: "闲置",
    value: 3,
  },
]);
// 部门
const department = ref([
  {
    label: "财务",
    value: "0",
  },
  {
    label: "会计",
    value: "1",
  },
  {
    label: "开发",
    value: "2",
  },
  {
    label: "销售",
    value: "3",
  },
]);
const departmentFormat = (val: number) => {
  return department.value.find((item: any) => item.value == val)?.label;
};
// 类别
const typeList = ref([
  {
    label: "生产设备",
    value: "0",
  },
  {
    label: "加工设备",
    value: "1",
  },
  {
    label: "办公设备",
    value: "2",
  },
  {
    label: "计算机设备",
    value: "3",
  },
  {
    label: "清洗设备",
    value: "4",
  },
]);
const typeFormat = (val: number) => {
  return typeList.value.find((item: any) => item.value == val)?.label;
};
const statusFormat = (val: number) => {
  return assetStatusList.value.find((item) => item.value == val)?.label;
};

// 一级==列表参数
const pageData = ref<assetManagementPageVO[]>();
// 查询函数
const handleQuery = () => {
  loading.value = true;
  console.log("queryParams", queryParams);

  assetManagementAPI
    .getPage(queryParams)
    .then((data) => {
      console.log(data);
      pageData.value = [...(data?.list as any)];
      console.log(pageData, "pageData");
      pageData.value.forEach((item: any) => {
        console.log(item, "value");
      });
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

// 一级==新增表单参数
const dialogFormData: assetManagementPageVO = reactive({
  name: "", // 资产名称
  type: "", // 类别
  brand: "", //品牌
  model: "", // 型号
  unit: "", //计量单位
  value: "", //资产价值
  purchaseDate: "", //采购日期
  useYears: "", //使用年限
  reportTime: "", //保修期限
  scrapDate: "", //报废时间
  supplier: "", //供应商
  useDept: "", //使用部门
  user: "", //使用人
  assetCategory: "", // 资产归属部门
  usePosition: "", //存放位置
  remark: "", //备注
  status: "", //状态
});

//一级弹窗=== 新增资产
const handleAdd = (row: any, type: any) => {
  console.log(row);
  if (type == "add") {
    dialogVisible.visible = true;
    dialogVisible.title = "新增资产";
    dialogVisible.type = "add";
    delete dialogFormData.code;
    delete dialogFormData.id;
  } else if (type == "edit") {
    dialogVisible.visible = true;
    dialogVisible.title = "修改资产";
    dialogVisible.type = "edit";
    let data = { ...row };
    nextTick(() => {
      Object.assign(dialogFormData, { ...data });
    });
  } else {
    dialogVisible.visible = true;
    dialogVisible.title = "查看资产";
    dialogVisible.type = "info";
    let data = { ...row };
    nextTick(() => {
      Object.assign(dialogFormData, { ...data });
    });
  }
};
// 资产删除操作
function handleDelete(row: any) {
  ElMessageBox.confirm("确定要删除该资产吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      console.log("删除", row);
      // 实现删除逻辑
      assetManagementAPI
        .deleteAsset({ id: row.id })
        .then((data) => {
          ElMessage.success("删除成功");
          handleQuery();
        })
        .catch((error) => {
          loading.value = false;
          //ElMessage.error(`${error}`); //注释报错弹窗
        });
    })
    .catch(() => {
      // 取消删除
    });
}
//一级弹窗===新增资产关闭
const handleCloseDialog = () => {
  console.log(dialogFormDataRef);
  dialogVisible.visible = false;
  dialogFormDataRef.value?.clearValidate();
  dialogFormDataRef.value?.resetFields();
};
//新增/修改保存==资产
const handleSave = () => {
  loading.value = true;
  console.log(dialogFormData);
  // 实现新增/修改逻辑
  dialogFormDataRef.value.validate((valid: boolean) => {
    if (!valid) {
      return;
    }
    const apiCall =
      dialogVisible.type === "add"
        ? assetManagementAPI.saveAddAsset
        : assetManagementAPI.updateAsset;
    apiCall(dialogFormData)
      .then((data) => {
        console.log(data);
        loading.value = false;
        dialogVisible.visible = false;
        ElMessage.success("保存成功");
        // handleRowClick(rowObj?.value, null, null);
        // clearDialogFormDataPro();
        handleQuery();
        // if (type == "save") {
        //   dialogPro.visible = false;
        // }
      })
      .catch((error) => {
        loading.value = false;
        //ElMessage.error(`${error}`); //注释报错弹窗
      });
  });
};

// 保养记录查询参数
const maintenanceParams = reactive<maintenancePageQuery>({
  pageNum: 1,
  pageSize: 10,
});
// 二级表单验证
const upkeepRules = reactive<Record<string, FormItemRule[]>>({
  maintenanceTime: [{ required: true, message: "请输入保养时间", trigger: "change" }],
  maintenanceRecord: [{ required: true, message: "请输入保养记录", trigger: "blur" }],
  nextMaintenanceTime: [{ required: true, message: "请输入下次保养时间", trigger: "change" }],
  dept: [{ required: true, message: "请输入部门", trigger: "blur" }],
  maintenanceUser: [{ required: true, message: "请输入保养人", trigger: "blur" }],
  status: [{ required: true, message: "请输入状态", trigger: "blur" }],
});
// 二级弹窗ref
const upkeepFormDataRef = ref(ElForm);
//二级==弹窗
const upkeepVisible = reactive({
  visible: false,
  title: "",
  type: "",
});
const upkeepStatus = ref([
  {
    label: "待保养",
    value: 0,
  },
  {
    label: "保养中",
    value: 1,
  },
  {
    label: "已完成",
    value: 2,
  },
]);
const upkeepStatusFormat = (val: number) => {
  return upkeepStatus.value.find((item) => item.value == val)?.label;
};
// 二级==列表参数
const upkeepData = ref<maintenancePageVO[]>();
// 二级==新增表单参数
const upkeepFormData: maintenancePageVO = reactive({
  assetCode: "", // 资产编号
  // code: "", // 保养编号
  dept: "", // 保养部门
  maintenanceRecord: "", //保养记录
  maintenanceTime: "", // 保养时间
  maintenanceUser: "", //保养人
  nextMaintenanceTime: "", //下次保养时间
  remark: "", //备注
  status: "", //状态
  // id: '', //
});
// 点击展示保养记录
const pageClick = (row: any) => {
  console.log(row);
  // upkeepVisible.visible = true;
  loading.value = true;
  maintenanceParams.assetCode = row.code ? row.code : row;
  assetManagementAPI
    .getMaintenancePage(maintenanceParams)
    .then((data) => {
      console.log(data);
      loading.value = false;
      upkeepData.value = [...(data?.list as any)];
      // ElMessage.success("删除成功");
      // handleQuery();
    })
    .catch((error) => {
      loading.value = false;
      //ElMessage.error(`${error}`); //注释报错弹窗
    });
};
// 保养操作
function handleMaintain(row: any, type: string) {
  console.log("保养", row);
  // 实现保养逻辑
  if (type == "add") {
    upkeepFormData.assetCode = row.code;
    // upkeepFormData.code = row.code;
    upkeepVisible.title = "新增资产保养";
    delete upkeepFormData.id;
  } else {
    upkeepFormData.assetCode = row.assetCode;
    // upkeepFormData.code = row.code;
    upkeepVisible.title = "查看资产保养";
    if (type == "edit") {
      upkeepVisible.title = "修改资产保养";
    }
    let data = { ...row };
    nextTick(() => {
      Object.assign(upkeepFormData, { ...data });
    });
  }
  console.log(upkeepFormData);

  upkeepVisible.visible = true;

  upkeepVisible.type = type;
}
//提交保养
function upkeepSave() {
  console.log("保存");
  console.log(upkeepFormData);
  // 实现新增/修改逻辑
  upkeepFormDataRef.value.validate((valid: boolean) => {
    if (!valid) {
      return;
    }
    const apiCall =
      upkeepVisible.type === "add"
        ? assetManagementAPI.saveAddMaintenance
        : assetManagementAPI.updateMaintenance;
    apiCall(upkeepFormData)
      .then((data) => {
        console.log(data);
        loading.value = false;
        upkeepVisible.visible = false;
        ElMessage.success("保存成功");
        pageClick(upkeepFormData.assetCode);
        // handleRowClick(rowObj?.value, null, null);
        // clearDialogFormDataPro();
        // handleQuery(true);
        // if (type == "save") {
        //   dialogPro.visible = false;
        // }
      })
      .catch((error) => {
        loading.value = false;
        //ElMessage.error(`${error}`); //注释报错弹窗
      });
  });
}
// 资产删除操作
function upkeepHandleDelete(row: any) {
  ElMessageBox.confirm("确定要删除该保养记录吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      console.log("删除", row);
      // 实现删除逻辑
      assetManagementAPI
        .deleteMaintenance({ id: row.id })
        .then((data) => {
          ElMessage.success("删除成功");
          pageClick({ code: row.assetCode });
        })
        .catch((error) => {
          //ElMessage.error(`${error}`); //注释报错弹窗
        });
    })
    .catch(() => {
      // 取消删除
    });
}

const dialogVisibleImage = ref(false);
const dialogImageUrl = ref("");

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  // queryParams.deptId = undefined;
  // queryParams.createTime = undefined;
  handleQuery();
}

onMounted(() => {
  handleQuery();
});

function upkeepCloseDialog() {
  upkeepVisible.visible = false;
  upkeepFormDataRef.value?.clearValidate();
  upkeepFormDataRef.value?.resetFields();
}

// 修改操作
function handleEdit(row: assetManagementPageVO) {
  console.log("修改", row);
  // 实现修改逻辑
}
</script>

<style lang="scss" scoped>
@use "@/styles/css/public" as *;
</style>
